/**
 * 自定义错误类
 * 提供各种业务场景的错误类型
 */

/**
 * 基础应用错误类
 */
export class AppError extends Error {
  statusCode: number;
  
  constructor(message: string, statusCode: number = 500) {
    super(message);
    this.statusCode = statusCode;
    this.name = this.constructor.name;
    Error.captureStackTrace(this, this.constructor);
  }
}

/**
 * 数据库操作错误
 */
export class DatabaseError extends AppError {
  constructor(message: string) {
    super(`数据库错误: ${message}`, 500);
    this.name = 'DatabaseError';
  }
}

/**
 * 业务验证错误
 */
export class ValidationError extends AppError {
  errors: Record<string, string>;
  
  constructor(message: string, errors: Record<string, string> = {}) {
    super(`验证错误: ${message}`, 400);
    this.name = 'ValidationError';
    this.errors = errors;
  }
}

/**
 * 认证错误
 */
export class AuthenticationError extends AppError {
  constructor(message: string = '认证失败') {
    super(message, 401);
    this.name = 'AuthenticationError';
  }
}

/**
 * 授权错误
 */
export class AuthorizationError extends AppError {
  constructor(message: string = '没有操作权限') {
    super(message, 403);
    this.name = 'AuthorizationError';
  }
}

/**
 * 资源不存在错误
 */
export class NotFoundError extends AppError {
  constructor(resource: string = '资源') {
    super(`${resource}不存在`, 404);
    this.name = 'NotFoundError';
  }
}

/**
 * 系统内部错误
 */
export class InternalError extends AppError {
  constructor(message: string = '系统内部错误') {
    super(message, 500);
    this.name = 'InternalError';
  }
}

/**
 * 服务不可用错误
 */
export class ServiceUnavailableError extends AppError {
  constructor(service: string = '服务') {
    super(`${service}暂时不可用，请稍后再试`, 503);
    this.name = 'ServiceUnavailableError';
  }
}

/**
 * 缓存操作错误
 */
export class CacheError extends AppError {
  constructor(message: string) {
    super(`缓存错误: ${message}`, 500);
    this.name = 'CacheError';
  }
}

/**
 * 文件操作错误
 */
export class FileError extends AppError {
  constructor(message: string) {
    super(`文件操作错误: ${message}`, 500);
    this.name = 'FileError';
  }
}

/**
 * 网络请求错误
 */
export class NetworkError extends AppError {
  constructor(message: string) {
    super(`网络请求错误: ${message}`, 500);
    this.name = 'NetworkError';
  }
}

/**
 * 接口限流错误
 */
export class RateLimitError extends AppError {
  constructor(message: string = '请求过于频繁，请稍后再试') {
    super(message, 429);
    this.name = 'RateLimitError';
  }
} 